/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ForumsPanel.java
 *
 * Created on 22/05/2011, 19:14:27
 */

package ClientGui;

import clientData.ForumData;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author USER
 */
public class ForumsPanel extends javax.swing.JPanel {

    /** Creates new form ForumsPanel */
    public ForumsPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        createForumButton = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        forumsList = new javax.swing.JList();
        AddText = new javax.swing.JTextField();
        addLable = new javax.swing.JLabel();
        AddButton = new javax.swing.JToggleButton();

        createForumButton.setText("Create New Forum");
        createForumButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createForumButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setForeground(new java.awt.Color(255, 51, 51));
        jLabel1.setText("Choose Forum:");

        forumsList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        forumsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        forumsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                forumsListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(forumsList);

        AddText.setEnabled(false);

        addLable.setText("Forum Name:");
        addLable.setEnabled(false);

        AddButton.setText("Add");
        AddButton.setEnabled(false);
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(createForumButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(AddButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(addLable)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(AddText, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(36, 36, 36)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(86, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(168, 168, 168)
                        .addComponent(createForumButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(addLable)
                            .addComponent(AddText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(AddButton)))
                .addContainerGap(47, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void createForumButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createForumButtonActionPerformed
        this.enableFields();
}//GEN-LAST:event_createForumButtonActionPerformed

    private void forumsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_forumsListMouseClicked
        ForumData fd = (ForumData)this.getList().getSelectedValue();
        if (fd!=null){
            this.setVisible(false);
            Controller.showForum(fd);
        }
}//GEN-LAST:event_forumsListMouseClicked

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        if (AddText.getText().equals("")){
            JOptionPane.showMessageDialog(new JFrame(), "You must fill in text field!" , "Dialog", JOptionPane.ERROR_MESSAGE);
            return;
        }
        Controller.addForum(this.AddText.getText());
        this.clearFields();
}//GEN-LAST:event_AddButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton AddButton;
    private javax.swing.JTextField AddText;
    private javax.swing.JLabel addLable;
    private javax.swing.JToggleButton createForumButton;
    private javax.swing.JList forumsList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    public javax.swing.JList getList() {
        return this.forumsList;
    }

    public void clearFields(){
        this.AddText.setText("");
        this.addLable.setEnabled(false);
        this.AddText.setEnabled(false);
        this.AddButton.setEnabled(false);
    }

    public void enableFields(){
        this.addLable.setEnabled(true);
        this.AddText.setEnabled(true);
        this.AddButton.setEnabled(true);
    }
}
